<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Portal\Controller;
use Common\Controller\HomebaseController;
use Label\Model\CategoryModel;
use Portal\Model\PostsModel;

class ListController extends HomebaseController {

	// 前台文章列表
	public function index() {
        $term_id=I('get.id',0,'intval');
		$par_id=I('get.pid',$term_id,'intval');
		$term=sp_get_term($term_id);

		if(empty($term)){
		    header('HTTP/1.1 404 Not Found');
		    header('Status:404 Not Found');
		    if(sp_template_file_exists(MODULE_NAME."/404")){
		        $this->display(":404");
		    }
		    return;
		}

		// 后台模板设置
		$tplname = $term["list_tpl"];
    	$tplname = sp_get_apphome_tpl($tplname, "list");

    	$this->assign($term);
    	$this->assign('cat_id', $term_id);
		$this->assign('par_id', $par_id);

		$show_tplname = ':index';
		if ($term_id == 12 || $par_id == 12 || $term_id == 16 || $par_id == 16) {
            $show_tplname = ":".$tplname;
        }

    	$this->display($show_tplname); // tplname - index,list
	}
	
	// 文章分类列表接口,返回文章分类列表,用于后台导航编辑添加
	public function nav_index(){
		$navcatname="文章分类";
        $term_obj= M("Terms");

        $where=array();
        $where['status'] = array('eq',1);
        $terms=$term_obj->field('term_id,name,parent')->where($where)->order('term_id')->select();
		$datas=$terms;
		$navrule = array(
		    "id"=>'term_id',
            "action" => "Portal/List/index",
            "param" => array(
                "id" => "term_id"
            ),
            "label" => "name",
		    "parentid"=>'parent'
        );
		return sp_get_nav4admin($navcatname,$datas,$navrule);
	}

    public function article_index()
    {
        $cat_id = I('get.cat_id', 0, 'intval');
        if (!$cat_id) {
            $all_category = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_ARTICLE, 'status' => 1])->limit(0)->select();
            $cat_id = $all_category[0]['id'];
        }

        $this->assign('cat_id', $cat_id);
        $this->display(':index');
	}


	// 快讯列表
    public function news_list()
    {
        $model      = M('category');
        $categorys  = $model->where(['type' => CategoryModel::CATEGORY_TYPE_NEWS, 'status' => 1])->select();
        $this->assign('categorys', $categorys);


        $cat_id = I('get.cat_id', 0, 'intval');
        $where = array(
            'p.post_type'       => PostsModel::POST_TYPE_NEWS,
            'p.post_status'     => PostsModel::POST_STATUS_NORMAL,
        );
        if ($cat_id) {
            $where[] = ' FIND_IN_SET (' . $cat_id . ', p.category )';
        }

        $model = M('posts');
        $count = $model->alias('p')->where($where)->count();
        $page  = $this->page($count, 20);

        $news_lists = M('posts')
            ->alias('p')
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order("p.post_date desc")
            ->select();
//        // 关联二维码
//        foreach ($news_lists as $k => $v) {
//            $url            = 'http://' . C('DOMAIN_NAME') . U('Portal/List/news_detail', ['id' => $v['id']]);
//            $v['qrcode']    = get_link_qrcode_path('news_detail_' . $v['id'], $url);
//            $news_lists[$k] = $v;
//        }

        // 早报
        $week = date('w', time());
        if ($week == 0) {
            $start_time = strtotime(date('Y-m-d 00:00:00', time())) - 24 * 3600 * 2;
            $end_time   = strtotime(date('Y-m-d 23:59:59', time())) - 24 * 3600 * 2;
        } elseif ($week == 6) {
            $start_time = strtotime(date('Y-m-d 00:00:00', time())) - 24 * 3600;
            $end_time   = strtotime(date('Y-m-d 23:59:59', time())) - 24 * 3600;
        } else {
            $start_time = time();
            $end_time   = time();
        }
        $where = [
            'post_type'     => PostsModel::POST_TYPE_DAILY_REPORT,
            'post_status'   => PostsModel::POST_STATUS_NORMAL,
            'post_date'     => [['LT', date('Y-m-d 23:59:59', $end_time)], ['EGT', date('Y-m-d 00:00:00', $start_time)]],
        ];
        $report = M('posts')->where($where)->find();
        $this->assign('report', $report);


        // 广告位
        $advertisement = get_ad_by_position(CategoryModel::SLIDESHOW_POSITION_NEWS_LIST);
        $this->assign('advertisement', $advertisement);

        $this->assign('cat_id', $cat_id);
        $this->assign("page", $page->show('default'));
        $this->assign('news_lists', $news_lists);
        $this->assign('total_pages', ceil($count / 20));
        $this->display(':listnews');
	}

	// 快讯详情
    public function news_detail()
    {
        $id     = I('get.id',0,'intval');
        $info   = M('posts')->where(['post_status' => PostsModel::POST_STATUS_NORMAL, 'post_type' => PostsModel::POST_TYPE_NEWS, 'id' => $id])->find();
        if ($id && $info) {
            // 接入分享sdk
            if (C('WX_SHARE')) {
                require_once './plugins/wx_jssdk.php';
                $jssdk       = new \JSSDK(C('WX_OFFICIAL_ACCOUNT.APP_ID'), C('WX_OFFICIAL_ACCOUNT.APP_SECRET'));
                $signPackage = $jssdk->GetSignPackage();
                $this->assign('signPackage', $signPackage);
            }
            // 分享图片
            $this->assign('share_pic_path', 'http://' . C('DOMAIN_NAME') . '/public/images/img/news_share.png');

            // 关联二维码
            $url            = 'http://' . C('DOMAIN_NAME') . U('Portal/List/news_detail', ['id' => $id]);
            $info['qrcode'] = get_link_qrcode_path('news_detail_' . $id, $url);

            // 原文链接
            if (strpos($info['post_source'], 'http') === 0) {
                $info['has_post_source'] = 1;
            } else {
                $info['has_post_source'] = 0;
            }

            $this->assign('info', $info);
            $this->display();
        } else {
            $this->error('快讯溜走了~~');
        }
	}

	// 移动端我的投稿二级页
    public function mobile_list_sub_menu()
    {
        $this->display();
	}
	

	// *****************  兼容老数据的一次性代码 *****************
    public function change_old_news_list()
    {
        $term_ids       = M('terms')->where(['parent' => 12])->select();
        $term_ids       = _array_column($term_ids, 'id');
        $term_ids[]     = '12';

        $posts_ids      = M('term_relationships')->where(['term_id' => ['in', $term_ids]])->field('GROUP_CONCAT(DISTINCT(object_id)) as posts_ids')->find();
        $posts_ids      = $posts_ids['posts_ids'];

        M('posts')->where(['id' => ['in', $posts_ids]])->save(['post_type' => PostsModel::POST_TYPE_NEWS]);
	}
}
